package com.bwie.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bwie.es.EsProd;
import com.bwie.mapper.TbProdMapper;
import com.bwie.pojo.TbProd;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.web.bind.annotation.*;

/**
 * @description :
 * @Author QiHainan
 * @Create 2025/3/28 20:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/prod/es")
public class EsController {
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private TbProdMapper tbProdMapper;

    @PostMapping("/createIndex")
    public R createIndex(){
        //检查索引是否存在，如果存在，删除索引
        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(EsProd.class);
        boolean exists = indexOps.exists();
        if(exists){
            indexOps.delete();
        }
        //创建索引
        indexOps.create();
        //手动创建映射
        Document mapping = indexOps.createMapping();
        indexOps.putMapping(mapping);

        return R.OK();
    }

    /**
     * 商品信息存储到ES中
     * @param prodId
     * @return
     */
    @PostMapping("/saveProdToEs/{prodId}")
    public R saveProdToEs(@PathVariable("prodId") Integer prodId){
        TbProd tbProd = tbProdMapper.selectById(prodId);
        if(tbProd == null){
            return R.ERROR(404,"商品不存在");
        }

        //
        EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
        //save即表示添加也表示修改
        elasticsearchRestTemplate.save(esProd);
        return R.OK();
    }

    /**
     * 读取Es中的商品信息
     * @param prodId
     * @return
     */
    @PostMapping("/getProdFromEs/{prodId}")
    public R getProdFromEs(@PathVariable("prodId") Integer prodId){
        EsProd esProd = elasticsearchRestTemplate.get("" + prodId, EsProd.class);
        if(esProd == null){
            return R.ERROR(404,"商品不存在");
        }
        return R.OK(esProd);
    }

    /**
     * 根据商品id从Es中删除商品信息
     * @param prodId
     * @return
     */
    @PostMapping("/deleteProdFromEs/{prodId}")
    public R deleteProdFromEs(@PathVariable("prodId") Integer prodId){
        elasticsearchRestTemplate.delete(""+prodId,EsProd.class);
        return R.OK();
    }

    /**
     * 添加商品
     * @param tbProd
     * @return
     */
    @PostMapping("/addProdToEs")
    public R addProdToEs(@RequestBody TbProd tbProd){
        tbProdMapper.insert(tbProd);

        //实时更新，同步信息，存入Es
        EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
        elasticsearchRestTemplate.save(esProd);
        return R.OK();
    }

    /**
     * 修改商品信息
     * @param tbProd
     * @return
     */
    @PostMapping("/updateProdFromEs")
    public R updateProdFromEs(@RequestBody TbProd tbProd){
        tbProdMapper.updateById(tbProd);

        //实时更新，同步信息，存入Es
        EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
        elasticsearchRestTemplate.save(esProd);
        return R.OK();
    }
}
